package com.ruanyun.modular.ry.controller;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.alibaba.fastjson.JSONObject;
import com.ruanyun.modular.ry.utils.COSUtil;
import com.ruanyun.modular.ry.utils.OSSUtil;
import com.ruanyun.sys.core.exception.enums.BizExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UploadController
 * @Description
 * @Author wjb520
 * @Date 2020/4/7 16:43
 * @Version 1.0
 */
@Slf4j
@Controller
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private OSSUtil ossUtil;

    @Value("${alistorage.host}")
    private String host;

    /**
     * 上传图片
     */
    @RequestMapping(method = RequestMethod.POST, path = "/uploads")
    @ResponseBody
    public ResponseData uploads(@RequestPart("file") MultipartFile picture) {
        JSONObject resObj = new JSONObject();
        String pictureURL = "";
        try {
            pictureURL = ossUtil.upload(picture);
        } catch (Exception e) {
            resObj.put("msg", "error");
            throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR);
        }
        resObj.put("msg", host + pictureURL);
        resObj.put("fileName", picture.getOriginalFilename());
        return ResponseData.success(resObj);
    }

    /**
     * 文件
     *
     * @param picture
     * @return
     * @throws IOException
     */
    @RequestMapping(method = RequestMethod.POST, path = "/cosUploads")
    @ResponseBody
    public ResponseData cosUploads(@RequestPart("file") MultipartFile picture) {
        JSONObject resObj = new JSONObject();
        String pictureURL = "";
        try {
            pictureURL = COSUtil.upload(picture);
        } catch (Exception e) {
            resObj.put("msg", "error");
            throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR);
        }
        resObj.put("msg", COSUtil.host + pictureURL);
        resObj.put("fileName", picture.getOriginalFilename());
        return ResponseData.success(resObj);
    }


    /**
     * @param picture
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, path = "/uploadService")
    @ResponseBody
    public ResponseData uploadService(@RequestPart("file") MultipartFile picture) {
        JSONObject resObj = new JSONObject();
        Map map = new HashMap();
        String pictureURL = "";
        try {
            pictureURL = ossUtil.upload(picture);
        } catch (Exception e) {
            resObj.put("msg", "error");
            throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR);
        }
        resObj.put("msg", host + pictureURL);

        map.put("code", 0);
        map.put("data", host + pictureURL);
        return ResponseData.success(map);
    }
}
